package cn.iocoder.yudao.module.ai.controller.admin.operationalarm;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import cn.iocoder.yudao.framework.exception.BusinessException;
import cn.iocoder.yudao.module.ai.constant.DicConstant;
import cn.iocoder.yudao.module.ai.controller.admin.operationequ.vo.OperationEquRespVO;
import cn.iocoder.yudao.module.ai.service.operationendcompany.OperationEndCompanyService;
import cn.iocoder.yudao.module.infra.util.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ai.controller.admin.operationalarm.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.operationalarm.OperationAlarmDO;
import cn.iocoder.yudao.module.ai.service.operationalarm.OperationAlarmService;

@Tag(name = "管理后台 - 运营中心-报警信息")
@RestController
@RequestMapping("/ai/operation-alarm")
@Validated
public class OperationAlarmController {

    @Resource
    private OperationAlarmService operationAlarmService;
    @Resource
    private OperationEndCompanyService operationEndCompanyService;
//
//    @PostMapping("/create")
//    @Operation(summary = "创建运营中心-报警信息")
//    @PreAuthorize("@ss.hasPermission('ai:operation-alarm:create')")
//    public CommonResult<Long> createOperationAlarm(@Valid @RequestBody OperationAlarmSaveReqVO createReqVO) {
//        return success(operationAlarmService.createOperationAlarm(createReqVO));
//    }
//
//    @PutMapping("/update")
//    @Operation(summary = "更新运营中心-报警信息")
//    @PreAuthorize("@ss.hasPermission('ai:operation-alarm:update')")
//    public CommonResult<Boolean> updateOperationAlarm(@Valid @RequestBody OperationAlarmSaveReqVO updateReqVO) {
//        operationAlarmService.updateOperationAlarm(updateReqVO);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除运营中心-报警信息")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('ai:operation-alarm:delete')")
//    public CommonResult<Boolean> deleteOperationAlarm(@RequestParam("id") Long id) {
//        operationAlarmService.deleteOperationAlarm(id);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得运营中心-报警信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:operation-alarm:query')")
    public CommonResult<OperationAlarmRespVO> getOperationAlarm(@RequestParam("id") Long id) {
        OperationAlarmDO operationAlarm = operationAlarmService.getOperationAlarm(id);
        OperationAlarmRespVO respVO = BeanUtils.toBean(operationAlarm, OperationAlarmRespVO.class);
        if(null != respVO){
            respVO.setCompanyName(operationEndCompanyService.getCompanyName(operationAlarm.getCompanyId()));
            respVO.setAlarmLevelName(DictFrameworkUtils.getDictDataLabel(DicConstant.key_operation_alarm_level, respVO.getAlarmLevel()));
            respVO.setHandleStatusName(DictFrameworkUtils.getDictDataLabel(DicConstant.key_operation_handle_status, respVO.getHandleStatus()));
        }
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得运营中心-报警信息分页")
    @PreAuthorize("@ss.hasPermission('ai:operation-alarm:query')")
    public CommonResult<PageResult<OperationAlarmRespVO>> getOperationAlarmPage(@Valid OperationAlarmSimplePageReqVO simplePageReqVO) {
        OperationAlarmPageReqVO pageReqVO = new OperationAlarmPageReqVO();
        org.springframework.beans.BeanUtils.copyProperties(simplePageReqVO, pageReqVO);
        if(StringUtils.isNotBlank(simplePageReqVO.getCompanyId())){
            List<String> ids = operationEndCompanyService.getSelfAndSubCompanyIds(simplePageReqVO.getCompanyId());
            pageReqVO.setCompanyIds(ids);
            if(CollectionUtils.isEmpty(ids)){
                throw new BusinessException("请选择正确的公司");
            }
        }
        pageReqVO.setAlarmType(simplePageReqVO.getAlarmType());
        pageReqVO.setAlarmLevel(simplePageReqVO.getAlarmLevel());
        pageReqVO.setCreateTime(new LocalDateTime[]{simplePageReqVO.getCreateTimeStart(), simplePageReqVO.getCreateTimeEnd()});
        pageReqVO.setHandleStatus(simplePageReqVO.getHandleStatus());
        PageResult<OperationAlarmDO> pageResult = operationAlarmService.getOperationAlarmPage(pageReqVO);
        PageResult<OperationAlarmRespVO> respVOPageResult = BeanUtils.toBean(pageResult, OperationAlarmRespVO.class);
        if(null != respVOPageResult && null != respVOPageResult.getList()){
            Map<String, String> companyIdAndNameMap = operationEndCompanyService.getCompanyIdAndNameMap(respVOPageResult.getList().stream().map(OperationAlarmRespVO::getCompanyId).collect(Collectors.toList()));
            respVOPageResult.getList().forEach(item -> {
                item.setCompanyName(companyIdAndNameMap.get(item.getCompanyId()));
                item.setAlarmLevelName(DictFrameworkUtils.getDictDataLabel(DicConstant.key_operation_alarm_level, item.getAlarmLevel()));
                item.setHandleStatusName(DictFrameworkUtils.getDictDataLabel(DicConstant.key_operation_handle_status, item.getHandleStatus()));
            });
        }
        return success(respVOPageResult);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出运营中心-报警信息 Excel")
//    @PreAuthorize("@ss.hasPermission('ai:operation-alarm:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportOperationAlarmExcel(@Valid OperationAlarmPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<OperationAlarmDO> list = operationAlarmService.getOperationAlarmPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "运营中心-报警信息.xls", "数据", OperationAlarmRespVO.class,
//                        BeanUtils.toBean(list, OperationAlarmRespVO.class));
//    }

}